﻿using System;
using System.IO;
using GDIT.Base.PageBaseHelper;
using GDIT.BLL;
using GDIT.Model;

namespace GDIT.page.manage.link
{
    public partial class imgLink : System.Web.UI.Page
    {

        private BPicInfo picInfo = null;
        private BPicType picType = null;
        private Mtb_PicInfo dbPicInfo = null;

        //设置上传文件的格式
        string[] imgExtension = new string[] { "gif", "jpg", "png", };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Session["UserName"].ToString()))
            {
                PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                return;
            }
            if (!IsPostBack)
            {
                BindImportantInfo();
                BindChangeNews();
                BindLinkPic();
            }
        }
        //绑定重大信息链接图片
        private void BindImportantInfo()
        {
            picInfo = new BPicInfo();
            this.ImportantRepeater.DataSource = picInfo.SelectPicInfo("重大信息");
            this.ImportantRepeater.DataBind();
        }
        //绑定轮换新闻链接图片
        private void BindChangeNews()
        {
            picInfo = new BPicInfo();
            this.NewsRepeater.DataSource = picInfo.SelectPicInfo("轮换新闻");
            this.NewsRepeater.DataBind();
        }
        //绑定链接图片
        private void BindLinkPic()
        {
            picInfo = new BPicInfo();
            this.NavRepeater.DataSource = picInfo.SelectPicInfo("链接图片");
            this.NavRepeater.DataBind();
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            /*提示*/
            string visible = string.Empty;
            if (!chkVisible.Checked)
            {
                visible = "否";
            }
            else
            {
                visible = "是";
            }

            if (string.IsNullOrEmpty(this.filPath.PostedFile.FileName))
            {
                PageBase.MessageBoxShow("上传文件为空,请重新上传！",Page);
                return;
            }
            //获取上传文件的名称
            string upFileName = filPath.FileName;

            //获取文件的扩展名
            string upExtension = upFileName.Substring(upFileName.LastIndexOf(".") + 1);

            try
            {
                //判断扩展名是否正确
                if (checkExtension(upExtension))
                {
                    dbPicInfo = new Mtb_PicInfo();
                    picInfo = new BPicInfo();
                    dbPicInfo.OID = Convert.ToInt32(txtId.Text.Trim());//ID编号
                    dbPicInfo.LinkAddress = txtAddress.Text.Trim();//图片链接
                    dbPicInfo.PicPath = "Admin/images/" + upFileName;//图片路径
                    dbPicInfo.Visible = visible;//图片是否可见
                    dbPicInfo.Alt = txtAlt.Text.Trim();
                    try
                    {
                        bool result = picInfo.UpdatePicInfo(dbPicInfo);
                        if (!result)
                        {
                            PageBase.MessageBoxShow("上传文件失败！",Page);
                            return;
                        }
                        //保存上传图片
                        filPath.SaveAs(Server.MapPath("~/Admin/images/") + upFileName);
                        PageBase.MessageBoxShow("上传文件成功！",Page);
                        //设置一个参数，使ie重新读取页面显示头像
                        string rd = DateTime.Now.Ticks.ToString();
                        //显示上传的图片
                        this.ShowImage.ImageUrl = "../../../Admin/images/" + upFileName + "?rd=" + rd;
                    }
                    catch (Exception ex)
                    {
                        PageBase.MessageBoxShow(ex.Message.ToString(),Page);
                    }
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow(ex.Message.ToString(),Page);
            }
        }

        /// <summary>
        /// 判断图片格式是否正确
        /// </summary>
        /// <param name="extension">输入的图片格式</param>
        /// <returns></returns>
        private bool checkExtension(string extension)
        {
            bool strReturn = false;
            //遍历数组判断当前上传的视频格式是否正确
            foreach (string var in this.imgExtension)
            {
                if (var == extension)
                {
                    strReturn = true;
                    break;
                }
            }
            return strReturn;
        }
    }
}